<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>vue 键盘事件</title>
</head>
<body>
<div id="app">
    <!--不使用键盘修饰符的情况-->
    <form action="">
        <label for="content">请输入内容
            <input type="text" name="content" id="content" v-on:keypress="keypressed">
        </label>
    </form>

    <!--使用键盘修饰-->
    <form action="">
        <label for="show">请输入内容
            <!--键盘修饰：需要处理哪个按钮就写上按键的名称-->
            <input type="text" name="content" id="show" v-on:keypress.enter="show">
        </label>
    </form>
</div>

<script type="module">
    import {createApp} from "../../../js/vue.esm-browser.js";

    let app = createApp(
        {
            setup() {

                function keypressed(event) {
                    // console.log(event);
                    if (event.key === "Enter") {
                        event.preventDefault()
                        let element = document.querySelector("#content");
                        console.log(element.value);
                    }
                }

                function show(event) {
                    // 代码冗余
                    /*
                    if (event.key === "Enter") {
                        event.preventDefault()
                        let element = document.querySelector("#show");
                        console.log(element.value);
                    }
                    */

                    event.preventDefault()
                    let element = document.querySelector("#show");
                    console.log(element.value);
                }

                return {
                    keypressed,
                    show,
                }
            }
        }
    );

    app.mount("#app")
</script>
</body>
</html>